﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;

namespace hnrzxt.Controllers
{
    public class RcpdController : Controller
    {
        private readonly ApplicationDbContext _db;

        public RcpdController(ApplicationDbContext db)
        {
            _db = db;
        }



        [HttpPost]
        public  string Create(Rcpd rcpd)
        {
            if (ModelState.IsValid)
            {
                _db.Add(rcpd);
                _db.SaveChanges();
            }
           return "";
        }

        public Dictionary<string, object> ShowRcpd(string Jbxxid)
        {
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add("code", 0);
            map.Add("data", GetRcpd(Jbxxid));


            return map;
        }
   
        public List<Dictionary<string, object>> GetRcpd(string Jbxxid)
        {
            List<Rcpd> rcpdlist = _db.Rcpds.Where(P => P.Jbxxid == Jbxxid).ToList();
            List<Dictionary<string, object>> list = new();
            for (int i = 0; i < rcpdlist.Count; i++)
            {
                Dictionary<string, object> map = new();
                map.Add("id",rcpdlist[i].Id);
                map.Add("DateTime", rcpdlist[i].DateTime.ToString("yyyy-MM-dd"));
                map.Add("PdName", rcpdlist[i].PdName);
                map.Add("PdResult", rcpdlist[i].PdResult);

                list.Add(map);
            }
            return list;
        }



        public string Del_rcpd(int id)
        {
            try
            {
                var rcpd = _db.Rcpds.Find(id);
                if (rcpd == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.Rcpds.Remove(rcpd);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }
    }
}
